package com.cqengine.utils;

import com.cqengine.entity.Position;
import com.cqengine.util.DateUtil;
import com.googlecode.cqengine.IndexedCollection;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

public class ObjectFactoryUtil {

    public static String getUUID() {
        return UUID.randomUUID().toString();
    }

    public static Long getRandomNumber() {
        int root = (int) Math.pow(11, 10);
        long id;
        do {
            long tmp = Math.abs(Double.doubleToLongBits(Math.random()));
            id = tmp % root;
        } while (id < (root / 10));
        return id;
    }

    public static String getRandomNumberStr() {
        int root = (int) Math.pow(11, 10);
        long id;
        do {
            long tmp = Math.abs(Double.doubleToLongBits(Math.random()));
            id = tmp % root;
        } while (id < (root / 10));
        return "" + id ;
    }

    public static Position getPosition(){
        Position pos = new Position();
        pos.setId(getRandomNumberStr());
        pos.setUserId(getRandomNumberStr());
        pos.setPrice(1000.0);
        pos.setQty(10);
        pos.setSymbol("BTCUSDT");
        pos.setIsTriggered(false);
        pos.setType(1);
        pos.setTs(DateUtil.getNow());
        return pos;
    }

    public static void initData(IndexedCollection<Position> posQueue, long count){
        List<Position> list = new ArrayList<>();
        for (long i = 1;i <= count;i++){
            Position position = new Position(i+"",ObjectFactoryUtil.getRandomNumberStr(),i,i,
                    "BTCUSDT",false,1, DateUtil.getNow(),"3000",3);
            list.add(position);
        }
        posQueue.addAll(list);
    }

    public static List<Position> deptCopyList(List<Position> list){
        List<Position> res = new ArrayList<>(list.size());
        for (Position position : list){
            res.add(position.clone());
        }
        return res;
    }
}
